package graficar;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RectangularShape;
import java.awt.geom.RoundRectangle2D;
import java.util.ArrayList;

import logicaDelNegocio.ConmutadorCentral;

public class GraficoTelefonoExterno extends Canvas {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	String id;
	double x;
	double y;
	final double RAD_X=0.0375;
	final double RAD_Y=0.0375;
	final double DIF_X=0.1;
	final double DIF_Y=0.1;
	final double CUR_X=0.01875;
	final double CUR_Y=0.025;
	double radioX;
    double radioY;
    double difX;
    double difY;
    double curvX;
    double curvY;
	
	
	public GraficoTelefonoExterno(){
		
	}
	
	public GraficoTelefonoExterno(String id){
		this.id=id;
	}
	
	public GraficoTelefonoExterno(double x, double y, double radioX, double radioY){
		this.x=x;
		this.y=y;
		this.radioX=radioX;
		this.radioY=radioY;
	}
	
	public int getX() {
		return (int) x;
	}


	public void setX(double x) {
		this.x = x;
	}


	public int getY() {
		return (int) y;
	}


	public void setY(double y) {
		this.y = y;
	}

	
	public String getId() {
		return id;
	}

	
	public void setId(String id) {
		this.id = id;
	}

	public void setRadioX(double radioX) {
		this.radioX = radioX;
	}

	public void setRadioY(double radioY) {
		this.radioY = radioY;
	}

	public void setDifX(double difX) {
		this.difX = difX;
	}

	public void setDifY(double difY) {
		this.difY = difY;
	}

	public void setCurvX(double curvX) {
		this.curvX = curvX;
	}

	public void setCurvY(double curvY) {
		this.curvY = curvY;
	}

	public void setTelExt(Diagrama lienzo , Ellipse2D eliPrincipal, GraficoConmutadorCentral cc){

		int ancho = lienzo.getWidth();
		int alto = lienzo.getHeight();
	    double radX=(int)(RAD_X*ancho);
	    double radY=(int)(RAD_Y*alto);
		
		
		for(int i=0; i<cc.getTe().size();i++){
			Double minX=Math.random()*ancho;
			Double minY=Math.random()*(.5*alto);
			if(!(eliPrincipal.contains(new Rectangle2D.Double(minX, minY, radX, radY))) &&
					!(eliPrincipal.intersects(new Rectangle2D.Double(minX, minY, radX, radY))) &&
					noSeIntersecta(new GraficoTelefonoExterno(minX, minY,radX,radY),cc)){
				cc.getTe().get(i).x=minX;
				cc.getTe().get(i).y=minY;
				cc.getTe().get(i).setRadioX((int)(RAD_X*ancho));
				cc.getTe().get(i).setRadioY((int)(RAD_Y*alto));
			}
			else{
				i-=1;
			}
		}

	}
	
	public Boolean noSeIntersecta(GraficoTelefonoExterno ci, GraficoConmutadorCentral cc){
		for(int i=0; i<cc.getTe().size();i++){
			if(new Rectangle2D.Double(ci.x,ci.y,ci.radioX,ci.radioY).intersects(
					new Rectangle2D.Double(cc.getTe().get(i).x,cc.getTe().get(i).y,cc.getTe().get(i).radioX,cc.getTe().get(i).radioY)))
				return false;
		}
		
		return true;
	}
	
	public void paint(Graphics g, ArrayList<GraficoTelefonoExterno> cc, Diagrama lienzo){	
	    
		int ancho = lienzo.getWidth();
		int alto = lienzo.getHeight();
	    Graphics2D g2d = (Graphics2D) g;
	    
	    for(int i=0;i<cc.size();i++){
	    	
	    	cc.get(i).setDifX((int)(DIF_X * cc.get(i).radioX));
			cc.get(i).setDifY((int)(DIF_Y * cc.get(i).radioY));
			cc.get(i).setCurvX(ancho*CUR_X);
			cc.get(i).setCurvY(alto*CUR_Y);
	    	
	    	RoundRectangle2D auxR1 = new RoundRectangle2D.Double(
	    			cc.get(i).x, 
	    			cc.get(i).y, 
	    			cc.get(i).radioX, 
	    			cc.get(i).radioY, 
					cc.get(i).curvX, 
					cc.get(i).curvY);
	    	
	    	RoundRectangle2D auxR2 = new RoundRectangle2D.Double(
	    			(cc.get(i).x)+(cc.get(i).difX),
		    		(cc.get(i).y)+(cc.get(i).difY),
		    		(cc.get(i).radioX)-2*(cc.get(i).difX),
		    		(cc.get(i).radioY)-2*(cc.get(i).difY),
		    		cc.get(i).curvX,
		    		cc.get(i).curvY);
	    	
	    	RoundRectangle2D auxR3 = new RoundRectangle2D.Double(
	    			(cc.get(i).x)+2*(cc.get(i).difX),
		    		(cc.get(i).y)+2*(cc.get(i).difY),
		    		(cc.get(i).radioX)-4*(cc.get(i).difX),
		    		(cc.get(i).radioY)-4*(cc.get(i).difY),
		    		(cc.get(i).curvX),
		    		(cc.get(i).curvY));
	    	
	    	g2d.setColor(Color.CYAN);
			g2d.fill(auxR1);

		    g2d.setColor(Color.WHITE);
		    g2d.fill(auxR2);

		    g2d.setColor(Color.CYAN);
		    g2d.fill(auxR3);
	    	
	    }
	    	
	}

}
